package com.yc2.Test01;

import com.yc2.config.configuration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;

/**
 * @ClassName test03
 * @since: 2023/7/25 19:10
 * @auth: kirito
 * @description:
 **/
public class test03 {
    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext context  = new AnnotationConfigApplicationContext(configuration.class);
        // 创建一个具有五个线程的线程池
        ExecutorService threadPool = context.getBean(ExecutorService.class);

            // 提交任务到线程池
            for (int i = 0; i < 5; i++) {
                threadPool.execute(new test02.TimeRunnable());
                Thread.sleep(1000);
            }

            // 关闭线程池
            threadPool.shutdown();
        }

        static class TimeRunnable implements Runnable {
            @Override
            public void run() {
                while (true){
                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String fileName = sdf.format(d);
                    System.out.println(Thread.currentThread().getName()+"  "+fileName);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
}
